package net.wsfcp.android.ui.view.my.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.FragmentAdapter;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.base.BasePresenter;
import net.wsfcp.android.event.AddressEvent;
import net.wsfcp.android.ui.view.my.fragment.AddAddressFragment;
import net.wsfcp.android.ui.view.my.fragment.AddressFragment;
import net.wsfcp.android.ui.widget.CustomScrollViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

//收货地址总页面
public class AddressActivity extends BaseActivity {

    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.ll_created)
    LinearLayout llCreated;
    @BindView(R.id.img_created)
    ImageView imgCreated;
    @BindView(R.id.txt_created)
    TextView txtCreated;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.img_add)
    ImageView imgAdd;
    @BindView(R.id.txt_add)
    TextView txtAdd;
    @BindView(R.id.vp_address)
    CustomScrollViewPager vpAddress;


    ArrayList<LinearLayout> linearLayouts = new ArrayList<>();
    ArrayList<ImageView> imageViews = new ArrayList<>();
    ArrayList<TextView> textViews = new ArrayList<>();//默认标题
    ArrayList<Integer> checkImgs = new ArrayList<>();//默认选中图
    ArrayList<Integer> uncheckImgs = new ArrayList<>();//默认未选中图
    ArrayList<Fragment> fragments = new ArrayList<>();

    int checkType = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected BasePresenter loadPresenter() {
        return null;
    }

    @Override
    protected void initData() {
        setmTitle(getString(R.string.fragment_my_address));
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initListener() {
        llCreated.setOnClickListener(this);
        llAdd.setOnClickListener(this);
        vpAddress.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0)
                    hideSoftKeyboard();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        initArrayView();
        initPager();

    }

    public void initArrayView() {
        linearLayouts.clear();
        imageViews.clear();
        textViews.clear();
        linearLayouts.add(llCreated);
        linearLayouts.add(llAdd);
        imageViews.add(imgCreated);
        imageViews.add(imgAdd);
        textViews.add(txtCreated);
        textViews.add(txtAdd);
        checkImgs.add(R.mipmap.address_icon_02);
        checkImgs.add(R.mipmap.address_icon_04);
        uncheckImgs.add(R.mipmap.address_icon_03);
        uncheckImgs.add(R.mipmap.address_icon_05);
    }

    public void initPager() {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        AddressFragment addressFragment = new AddressFragment();
        AddAddressFragment addAddressFragment = new AddAddressFragment();
        fragments.add(addressFragment);
        fragments.add(addAddressFragment);
        fragmentAdapter.setmFragment(fragments);
        vpAddress.setAdapter(fragmentAdapter);//建立连接
    }


    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.ll_created:
                checkType = 0;
                break;
            case R.id.ll_add:
                checkType = 1;
                EventBus.getDefault().post(new AddressEvent(3));
                break;

        }
        checkLayout(checkType);
    }


    private void checkLayout(int checkType) {
        vpAddress.setCurrentItem(checkType);
        for (int i = 0; i < linearLayouts.size(); i++) {
            if (i == checkType) {
                linearLayouts.get(i).setBackgroundResource(R.drawable.bill_check);
                imageViews.get(i).setImageResource(checkImgs.get(i));
                textViews.get(i).setTextColor(getResources().getColor(R.color.white));
            } else {
                linearLayouts.get(i).setBackgroundResource(R.drawable.bill_uncheck);
                imageViews.get(i).setImageResource(uncheckImgs.get(i));
                textViews.get(i).setTextColor(getResources().getColor(R.color.theme_color));
            }
        }


    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(AddressEvent event) {
        switch (event.getCode()) {
            case 0:   // 删除
                checkType = 0;
                checkLayout(checkType);
                break;
            case 1:   // 新增
                checkType = 0;
                checkLayout(checkType);
                break;
            case 2:   // 修改
                checkType = 1;
                checkLayout(checkType);
                break;
            case 10:   // 修改数量
                txtCreated.setText(getResources().getString(R.string.address_created) + "(" + event.getNum() + ")");
                break;

        }

    }


    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {
        InputMethodManager inputMethodManager =
                (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
